package com.drug.mingkang.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.drug.mingkang.model.AuditRecord;
import com.drug.mingkang.model.HqPdetail;
import com.drug.mingkang.model.HqPlan;
import com.drug.mingkang.service.AuditRecordService;
import com.drug.mingkang.service.HqPdetailService;
import com.drug.mingkang.service.HqPlanService;
import com.drug.mingkang.utils.AuditStatus;
import com.drug.mingkang.utils.ResponseMessage;
import com.drug.mingkang.utils.Tools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 生产计划单(HqPlan)表控制层
 *
 * @author xqj
 * @since 2020-08-11 12:00:13
 */
@Api(value = "生产计划控制层",tags = "生产计划Contorller")
@RestController
@RequestMapping("hqPlan")
public class HqPlanController {
    /**
     * 服务对象
     */
    @Resource
    private HqPlanService hqPlanService;

    @Resource
    private HqPdetailService hqPdetailService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "根据计划单id查询数据", notes = "输入计划单id", httpMethod = "GET", response = HqPlan.class)
    @GetMapping("selectOne")
    public HqPlan selectOne(String id) {
        return this.hqPlanService.queryById(id);
    }


    /**
     * 根据计划单id删除
     * @param planId
     * @return
     */
    @ApiOperation(value = "根据计划单id删除",notes = "输入planId",httpMethod = "GET",response = ResponseMessage.class)
    @PostMapping("deleteById")
    public ResponseMessage deleteById(String planId){

        ResponseMessage responseMessage = hqPlanService.deleteById(planId)==true?ResponseMessage.success():ResponseMessage.error();

        return responseMessage;
    }

    /**
     * 查询全部并分页
     * @param hqPlan
     * @return
     */
    @ApiOperation(value = "查询所有计划单数据并分页",notes = "输入分页数据page和limit",httpMethod = "GET",response = Map.class)
    @GetMapping("findAll")
    public Map findAll(HqPlan hqPlan){
        List<HqPlan> list = hqPlanService.queryAllByLimit(hqPlan);

        int count = hqPlanService.getCount(hqPlan);


        Map map = new Tools().limit(list,count);

        return map;
    }


    /**
     * 增加和修改
     * @param hqPlan
     * @return
     */
    @ApiOperation(value = "增加或修改",notes = "传入计划单对象成员变量,pLanId为空时增加,不为空时修改",httpMethod = "POST",response = ResponseMessage.class)
    @PostMapping("insert")
    public ResponseMessage insert(HqPlan hqPlan){

        ResponseMessage responseMessage = null;
        if(hqPlan.getPlanId()==null || hqPlan.getPlanId().equals("")){
            responseMessage= hqPlanService.insert(hqPlan)>0?ResponseMessage.success("操作成功",hqPlan.getPlanId()):ResponseMessage.error();
        }else{
            responseMessage = hqPlanService.update(hqPlan)>0?ResponseMessage.success():ResponseMessage.error();
        }

        return responseMessage;
    }


    /**
     * 提交审核
     * @param jsonStr
     * @return
     */
    @ApiOperation(value = "提交审核申请",notes = "传入一个对象数组的JSON字符串",httpMethod = "POST",response = ResponseMessage.class)
    @PostMapping("submitAudit")
    public ResponseMessage submitAudit(String jsonStr){
        ResponseMessage  responseMessage = new ResponseMessage();
        List<String> plan = new ArrayList<>();
        List<HqPlan> hqPlans = new Tools().jsonToList(jsonStr,HqPlan.class);

        for(HqPlan hqPlan : hqPlans){
            List<HqPdetail> list = this.hqPdetailService.queryById(hqPlan.getPlanId());
            if(list.size()<1){
                plan.add(hqPlan.getPlanName());
            }
        }
        if(plan.size()<1){
            responseMessage = this.hqPlanService.updateBatch(hqPlans);
        }else{
            responseMessage = ResponseMessage.error("请先进行药品操作",plan);
        }


        return responseMessage;
    }

    /**
     * 首页增加计划单和计划明旭
     * @param wl
     * @param planJson
     * @return
     */
    @ApiOperation(value = "首页增加计划单和计划明旭",notes = "传入计划单信息和计划明细的数据Json字符串",httpMethod = "POST",response = ResponseMessage.class)
    @PostMapping("insertPlanDetails")
    public ResponseMessage insertPlanDetails(String wl,String planJson){
        List<HqPdetail> list = new Tools().jsonToList(wl,HqPdetail.class);

        HqPlan hqPlan = new Tools().getObject(planJson,HqPlan.class);

        ResponseMessage responseMessage = this.hqPlanService.insertPlanDetails(list,hqPlan);

        return responseMessage;

    }

}